package com.elitel.frame.web.cfgsqlext.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.elitel.frame.business.service.impl.SqlUtlil;
import com.elitel.frame.web.cfgsqlext.dao.CfgSqlExtDao;
import com.elitel.frame.web.cfgsqlext.entity.CfgSqlExt;
import com.elitel.frame.web.cfgsqlext.service.CfgSqlExtService;
import com.elitel.frame.web.serverinfo.dto.ServerQueryFieldDTO;
import com.elitel.frame.web.serverinfo.dto.ServerShowFieldDTO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 脚本服务基本信息表(CfgExtsql)表服务实现类
 *
 * @author makejava
 * @since 2025-05-19 16:42:16
 */
@Service
public class CfgSqlExtServiceImpl implements CfgSqlExtService {
    @Resource
    private CfgSqlExtDao cfgSqlExtDao;

    /**
     * 通过ID查询单条数据
     *
     * @param guid 主键
     * @return 实例对象
     */
    @Override
    public CfgSqlExt queryById(String guid) {
        return this.cfgSqlExtDao.queryById(guid);
    }

    /**
     * 新增数据
     *
     * @param cfgSqlExt 实例对象
     * @return 实例对象
     */
    @Override
    public CfgSqlExt insert(CfgSqlExt cfgSqlExt) {
        this.cfgSqlExtDao.insert(cfgSqlExt);
        return cfgSqlExt;
    }

    /**
     * 修改数据
     *
     * @param cfgSqlExt 实例对象
     * @return 实例对象
     */
    @Override
    public CfgSqlExt update(CfgSqlExt cfgSqlExt) {
        this.cfgSqlExtDao.update(cfgSqlExt);
        return this.queryById(cfgSqlExt.getGuid());
    }

    /**
     * 通过主键删除数据
     *
     * @param guid 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String guid) {
        return this.cfgSqlExtDao.deleteById(guid) > 0;
    }

    @Override
    public Map<String, Object> analyse(String sql, String dbKey) throws Exception {
        Map<String, Object> parseRes = SqlUtlil.parseSql(dbKey, sql);
        Map<String, String> selectItems = (Map<String, String>) parseRes.get("selectItems");
        ArrayList<ServerShowFieldDTO> showFieldList = new ArrayList<>();
        selectItems.forEach((fieldCode, fieldType) -> {
            ServerShowFieldDTO dto = new ServerShowFieldDTO();
            dto.setMetaFieldCode(fieldCode);
            dto.setFieldCode(fieldCode);
            dto.setFieldName(fieldCode);
            dto.setFieldType(fieldType);
            showFieldList.add(dto);
        });

        List whereParamList = (List) parseRes.get("whereParam");
        ArrayList<ServerQueryFieldDTO> queryFieldList = new ArrayList<>();
        for (Object where : whereParamList) {
            JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(where));
            ServerQueryFieldDTO dto = new ServerQueryFieldDTO();
            String fieldCode = jsonObject.get("name") + "";
            String fieldType = jsonObject.get("javaType") + "";
            dto.setMetaFieldCode(fieldCode);
            dto.setFieldCode(fieldCode);
            dto.setFieldName(fieldCode);
            dto.setFieldType(fieldType);
            dto.setRequiredFlag(Boolean.parseBoolean(jsonObject.get("required") + "") ? "1" : "0");
            queryFieldList.add(dto);
        }


        HashMap<String, Object> res = new HashMap<>();
        res.put("queryFieldList", queryFieldList);
        res.put("showFieldList", showFieldList);
        res.put("tableCodes", parseRes.get("tables"));
        return res;
    }
}
